package com.cxq.finalproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;

import com.cxq.finalproject.databinding.ActivityShowDetailBinding;
import com.cxq.finalproject.util.GetImgIDByName;
import com.cxq.finalproject.util.MyClothHelper;

public class ShowDetailActivity extends AppCompatActivity {
    private String imgSrc;
    private int srcId;
    private String price;
    private String sale;
    private String introduce;
    private SQLiteDatabase db;
    private SharedPreferences sp;
    private MyClothHelper myClothHelper;
    private ActivityShowDetailBinding binding;
    private GetImgIDByName getImgIDByName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_detail);
        imgSrc = getIntent().getStringExtra("imgSrc");//获取参数
        binding = DataBindingUtil.setContentView(this,R.layout.activity_show_detail);
        getImgIDByName = new GetImgIDByName(imgSrc);
        binding.imageView3.setImageResource(getImgIDByName.getResId());
        myClothHelper = new MyClothHelper(this, myClothHelper.DB_NAME,null,1);
        setContent();
        binding.textView4.setText("￥ " + price);
        binding.textView6.setText("月售 " + sale);
        binding.textView7.setText("简介：" + introduce);
        sp = getSharedPreferences("Login_UserInfo", MODE_PRIVATE);
        //监听租借按钮
        binding.button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db = myClothHelper.getWritableDatabase();
                String userId = sp.getString("userId","");
                String isload = "1";
                ContentValues values = new ContentValues();
                values.put(MyClothHelper.Name, imgSrc);
                values.put(MyClothHelper.Isload, isload);
                values.put(MyClothHelper.LoadUserId, userId );
                int sale_add = Integer.parseInt(sale) + 1;
                values.put(MyClothHelper.Sale, String.valueOf(sale_add) );
                if(!userId.equals("")){
                    long rid = db.update(MyClothHelper.TABLE_NAME,values,"name=?",
                            new String[]{imgSrc});
                }
                db.close();
                Intent intent = new Intent(ShowDetailActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    public void setContent(){
        db = myClothHelper.getWritableDatabase();
        Cursor c = db.query(myClothHelper.TABLE_NAME, null, "name=?",new String[] {imgSrc}, null, null, null, null);
        while (c.moveToNext()){
            price = c.getString(1);
            sale = c.getString(5);
            introduce = c.getString(4);
//            Toast.makeText(this,c.getString(6),Toast.LENGTH_SHORT).show();
        }
        db.close();
    }
}